<!--别忘记form 以及form的class-->
    <form class="layui-form">
        

        <input type="hidden" name="role_id" id="role_id" value="<%=role_id%>"/>
        <div id="xtree3" class="xtree_contianer"></div>

        
    </form>

<script type="text/javascript">
var xtree3;
var currentPermissions;
layui.use(['form', 'tree'], function () {
    var form = layui.form;
    var tree = layui.tree;
   $.ajax({ 
          url:context+'/api/sysrole/get?role_id='+$('#role_id').val(), 
          dataType:'json', 
          type:'get',
           success:function(res){   
            currentPermissions= res.data.sysPermissions;
                $.ajax({ 
                url:context+'/api/syspermission/list?tree=true', 
                dataType:'json', 
                type:'get',
                 success:function(data){    

                  var parentArr = listArr(data.data);
                  
                 xtree3 = new layuiXtree({
                    elem: 'xtree3'                  //必填
                    , form: form                    //必填
                    , data: parentArr       //必填
                    , isopen: false  //加载完毕后的展开状态，默认值：true
                    , ckall: false    //启用全选功能，默认值：false
               
                });

                  
            }
         });

              
        }
     });
}); 


function isHavePermission(permissionId){
    for (var i = 0; i < currentPermissions.length; i++) {
        if (currentPermissions[i].permission_id == permissionId) {
          return true;
        }
    }
    return false;
}

function listArr(arr){
  var root = new Array();
  if (arr!=null||arr.length>0) {
    for (var i = 0; i < arr.length; i++) {
      var item = {};
      item.title = arr[i].permissionName;
      item.value = arr[i].permission_id;
      item.checked = isHavePermission(arr[i].permission_id);

      if (arr[i].children!=null) {
        listChildArr(arr[i].children,item);
      }
        root[i] = item;
    }
 
  }
  return root;
}
function listChildArr(arr,parent){
    parent.data = [];
    for (var i = 0; i < arr.length; i++) {
      var item = {};
      item.title = arr[i].permissionName;
      item.value = arr[i].permission_id;
      item.checked = isHavePermission(arr[i].permission_id);
      if (arr[i].children!=null) {
        listChildArr(arr[i].children,item);
      }
      parent.data[i] = item;
    }
  
} 
</script>